/**
 * @Description: TODO
 * @author Mengting Lu
 * @date 2015年7月27日 下午3:43:32
 */
package com.shanghaionstar.user;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.more.entity.ContentHeaderType;
import com.onstar.cn.more.response.GetAllContentListResponseType;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MoreActivity;
import com.shanghaionstar.user.adapter.OtherContentListAdapter;
import com.shanghaionstar.user.iview.IGuideView;
import com.shanghaionstar.user.presenter.GuidePresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.List;

/**
 * @author Mengting Lu
 * @ClassName: GuideFragment
 * @Description: 指南详细页面
 * @date 2015年7月27日 下午3:43:32
 */
public class GuideFragment extends BaseFragment implements IGuideView, OnClickListener {
    @FindViewById(R.id.content_list_progress)
    private ProgressLayout progressView;
    @FindViewById
    private TopLayout top_layout;
    @FindViewById(value = R.id.assistant_content_listview)
    private ListView title_list;
    @FindViewById
    private TextView no_ovd_datas;
    @FindViewById
    private RadioButton story_rb;
    @FindViewById
    private RadioButton service_rb;
    @FindViewById
    private RadioButton intr_rb;

    private GuidePresenter presenter;
    private MoreActivity parent;
    private String[] mTitles;

    public GuideFragment() {

    }

    /**
     * (non-Javadoc)
     * <p>
     * Title: getLayoutId
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @return
     * @see com.shanghaionstar.BaseFragment#getLayoutId()
     */
    @Override
    public int getLayoutId() {
        return R.layout.user_guide;
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        parent = (MoreActivity) activity;
    }

    //	private int currentPos = 0;

    /**
     * (non-Javadoc)
     * <p>
     * Title: onActivityCreated
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param savedInstanceState
     * @see com.shanghaionstar.BaseFragment#onActivityCreated(Bundle)
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bun = getArguments();
        if (isFirstLoad()) {
            initView();
            btnBackClick(top_layout);
            presenter = new GuidePresenter(parent.getScheduler());
            presenter.setView(this);
            presenter.initData(bun);
//            title_list.setOnItemClickListener(this);

            title_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    presenter.onItemClick(position);
                }
            });
        }
    }

    private void initView() {
        mTitles = getResources().getStringArray(R.array.guide_items);
        story_rb.setText(mTitles[0]);
        service_rb.setText(mTitles[1]);
        intr_rb.setText(mTitles[2]);
    }

//    @Override
//    public void onDestroyView() {
//        super.onDestroyView();
//    }

//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        top_layout = null;
//    }

    @OnClick({R.id.story_rb, R.id.service_rb, R.id.intr_rb})
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.story_rb) {
            presenter.onTitleClick(0);
        } else if (view.getId() == R.id.service_rb) {
            presenter.onTitleClick(1);
        } else if (view.getId() == R.id.intr_rb) {
            presenter.onTitleClick(2);
        }
    }


    @Override
    public void setLoading(boolean isLoading) {
        progressView.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        AlertDialogUtil.showDialogByOnstarException(parent, e);

    }

    @Override
    public void onReponseSuccess(GetAllContentListResponseType object) {

    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

    @Override
    public void performClick(int pos) {
        if (pos == 2)
            intr_rb.performClick();
    }

    @Override
    public void showList(List<ContentHeaderType> list) {
        title_list.setVisibility(View.VISIBLE);
        no_ovd_datas.setVisibility(View.GONE);
        title_list.setAdapter(new OtherContentListAdapter(list, getActivity()));

    }

    @Override
    public void showNoData() {
        title_list.setVisibility(View.GONE);
        no_ovd_datas.setVisibility(View.VISIBLE);
        if (DeviceUtil.isChineseLanguage()) {
            String noData = presenter.getNoData(mTitles);
            no_ovd_datas.setText(noData);
        } else {
            no_ovd_datas.setText(R.string.option_guide_no_data);
        }
    }
}
